/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cliente.gui;

import br.ufpi.domain.Usuario;
import java.util.ArrayList;
import br.ufpi.domain.Pergunta;
import br.ufpi.domain.Resposta;
import br.ufpi.persistenci.HibernateUtil;
import java.util.Collection;
import java.util.Date;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import ufpi.br.dao.PerguntaDao;
import ufpi.br.dao.RespostaDao;
import ufpi.br.dao.UsuarioDao;
import util.Datas.Datas;

/**
 *
 * @author cleiton
 */
public class Perguntas {

    public Perguntas() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    public Collection<Object> getPerguntas(int inicioIntervalo, int maximo, String matricula, boolean isMinhasPerguntas, String respondidas, String frequencia) {
        Criteria criteria = HibernateUtil.getSessao().createCriteria(Pergunta.class);
        ArrayList<Object> objetos = new ArrayList<Object>();
        Criterion isrespondidas = null;
        Criterion between = null;
        UsuarioDao dao = new UsuarioDao();
        LogicalExpression expression = null;
        Usuario usuario = null;
        ArrayList<Integer> status = new ArrayList<Integer>();
        usuario = dao.buscaMatricula(matricula);
        if (respondidas.equals("T")) {
            //TODO Analisar se os Numeros estao corretos
            //Todas as perguntas
            Integer statusUsuario[] = {0, 4, 5};
            expression = Restrictions.and(Restrictions.eq("usuario", usuario), Restrictions.in("status", statusUsuario));
            status.add(2);
            status.add(1);
            status.add(3);
        } else {
            if (respondidas.equals("R")) {
                //Retorna apenas as perguntas Respondidas

                status.add(2);
            } else {
                //Retorna as perguntas não respondidas
                status.add(3);
            }
            isrespondidas = Restrictions.in("status", status);

        }
        if (isrespondidas == null) {
            expression = Restrictions.or(Restrictions.in("status", status), expression);
        } else {
            expression = Restrictions.or(Restrictions.in("status", status), isrespondidas);
        }

        if (frequencia.equals("T")) {
        } else {
            Date date = new Date();
            Date dateInstance = new Date();
            util.Datas.Datas data = new util.Datas.Datas();
            if (frequencia.equals("H")) {
            } else {
                if ((frequencia.equals("M"))) {
                    date = data.configurarInicioMes(date);
                } else {
                    date = data.configurarInicioSemana(date);
                }

            }
            between = Restrictions.between("dataEnvio", date, dateInstance);
            expression = Restrictions.and(between, expression);
        }
        if (isMinhasPerguntas) {
            expression = Restrictions.and(Restrictions.eq("usuario", usuario), expression);
        }
        criteria.add(expression);
        criteria.addOrder(Order.desc("id"));
        Criteria criteriatotal = HibernateUtil.getSessao().createCriteria(Pergunta.class);
        criteriatotal.add(expression);
        objetos.add((Integer) criteriatotal.setProjection(Projections.rowCount()).uniqueResult());
        criteria.setMaxResults(maximo);
        // criteria.setFirstResult(inicioIntervalo);
        objetos.add(criteria.list());
        return objetos;
    }

    

    @Test
    @Ignore
    public void todasPerguntas() {
        ArrayList<Object> objects = (ArrayList<Object>) this.getPerguntas(0, 40, "123", true, "T", "M");
        ArrayList<Pergunta> perguntas = (ArrayList<Pergunta>) objects.get(1);
        Integer numeromaximo = (Integer) objects.get(0);
        System.out.println("Total=" + numeromaximo);
        for (Pergunta pergunta : perguntas) {
            System.out.println(pergunta.getId());
        }
    }

     @Test
    public void totalpontos() {
         System.out.println("Total de pontos");
        UsuarioDao usuarioDao= new UsuarioDao();
        Datas datas= new Datas();
        System.out.println(usuarioDao.getMeusPontos("123",datas.configurarInicioSemana(new Date()),new Date() ));
    }

    @Test
    public void buscaAssunto() {
        System.out.println("Buscar Asssunto");
        PerguntaDao dao = new PerguntaDao();
        System.out.println(dao.buscarPorAssunto("a","123"));
    }

}
